<template>
  <div class="app-container mylot">
    <el-form ref="lotForm" v-loading="loading" class="form-goods" :model="temp" label-position="left" label-width="100px">
      <el-form-item label="LOGO">
        <el-upload
          class="avatar-uploader"
          :auto-upload="false"
          accept="image/*"
          action="api"
          :show-file-list="false"
          :file-list="logoList"
          :on-change="handleThumbChange"
          list-type="picture-card"
        >
          <img v-if="temp.logo" :src="temp.logo" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
          <div slot="tip" class="el-upload__tip">大小不超过500kb，如需更改，点击重新选择图片即可</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="简介" prop="intro">
        <el-input v-model.trim="temp.intro" style="max-width: 500px" type="textarea" :autosize="{ minRows: 1, maxRows: 2 }" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" type="primary" @click="handleSubmit">保存</el-button>
    </div>
  </div>
</template>
<script>
import { getLogo, setLogo } from '@/api/admin'
export default {
  data () {
    return {
      loading: false,
      logoData: new FormData(),
      temp: {
        logo: '',
        intro: ''
      },
      logoList: [],
      btnLoading: false
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    fetchData () {
      this.loading = true
      getLogo().then(res => {
        this.temp = res.data
        this.logoList = [{ url: res.data.logo }]
        this.loading = false
      }).catch(err => {
        console.log(err)
        this.loading = false
      })
    },
    handleThumbChange (file, fileList) {
      const isLt = file.size / 1024 / 1024 < 0.5
      if (!isLt) {
        // fileList = []
        this.$message.error('上传拍品缩略图大小不能超过 500kb!')
      } else {
        this.logoData.set('logo', file.raw)
        this.temp.logo = URL.createObjectURL(file.raw)
      }
    },
    handleSubmit () {
      this.logoData.set('intro', this.temp.intro)
      this.$refs['lotForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          setLogo(this.logoData).then(res => {
            this.temp = res.data
            this.btnLoading = false
            this.logoData = new FormData()
            this.logoList = []
            this.$notify({
              message: res.msg,
              type: 'success',
              duration: 2000
            })
          }).catch(err => {
            this.btnLoading = false
            console.log(err)
          })
        }
      })
    }
  }
}
</script>
<style lang="scss">
.mylot {
  .avatar-uploader {
    .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      &:hover {
        border-color: #009788;
      }
    }
    &-icon {
      font-size: 28px;
      color: #8c939d;
      width: 148px;
      height: 148px;
      line-height: 148px;
      text-align: center;
    }
  }
  .avatar {
    width: 148px;
    height: 148px;
    display: block;
  }
  .el-upload__tip{
    line-height: 20px;
    margin-top: 5px;
  }
}
</style>
